const Koa = require('koa');
const app = new Koa();
var Router = require('koa-router');
var router = new Router();
var view = require('koa-view');
var json = require('koa-json');
var utils = require('./utils');

const staticServe = require('koa-static');

const bodyParser = require('koa-bodyparser')

router = require('./router.js')(router);

utils.createFolder('files');
utils.createFolder('www');

app.use(async (ctx, next) => {
  const start = new Date();
  await next();
  const ms = new Date() - start;
  console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
});

app.use(json());

app.use(async (ctx, next) => {
  // 初始化manage
  await next();

})
let port = 4001;

app.use(view(__dirname + '/views'));
// app.use(bodyParser());
app.use(staticServe('static'));
app
  .use(router.routes())
  .use(router.allowedMethods());

app.listen(port);
console.log(`server is running on ${port}`)
